﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using VNPT;
using VNPT.Utility.Oracle;
using VNPT.Data;

namespace VNPT.Data
{
    public partial class fOracleConnect_TNS : DevExpress.XtraEditors.XtraForm
    {
        public fOracleConnect_TNS()
        {
            InitializeComponent();
            this.Shown += (a, e) =>
                {
                    LoadTSName();
                    LoadData();
                };
        }
        public string GetConnectionString()
        {
            this.ShowDialog();
            return DataSetting.CONNECTSTRING_TNS_NAME;
        }
        private void LoadTSName()
        {
            TNSNamesReader tsn = new TNSNamesReader();

            lblPath.Text = tsn.GetPathToTNSNamesFile();
            List<string> li = new List<string>();
           foreach (var item in tsn.GetOracleHomes())
           {
               li.AddRange(tsn.LoadTNSNames(item));
           }
           cmbTNS_Name.Properties.Items.AddRange(li);
        }

        private void LoadData()
        {
            if (DataSetting.IS_SAVE)
            {
                chkSave.CheckState = CheckState.Checked;
                txtPass.EditValue = DataSetting.PWD;
            }
            else
            {
                chkSave.CheckState = CheckState.Unchecked;
            }
            txtPass.EditValue = DataSetting.PWD;
            txtUser.EditValue = DataSetting.UID;
            cmbTNS_Name.EditValue = DataSetting.TNS_NAME;
        }

        void SaveData()
        {
            if (chkSave.CheckState == CheckState.Checked)
            {
                DataSetting.IS_SAVE = true;
                DataSetting.PWD = txtPass.EditValue.ToString();
            }
            else
            {
                DataSetting.IS_SAVE = false;
                DataSetting.PWD = "";
            }
             
             DataSetting.UID = txtUser.EditValue.ToString();
             DataSetting.TNS_NAME = cmbTNS_Name.EditValue.ToString();
        }
        string strCnn = "Data Source={0};User Id={1};Password={2};";
        private bool CheckConnect()
        {
            var cnn = string.Format(strCnn,cmbTNS_Name.EditValue.ToString(),txtUser.EditValue.ToString(), txtPass.EditValue);
            DbNetData Db = new DbNetData(cnn, DataProvider.OracleClient);
            try
            {
                Db.Open();
                return true;
            }
            catch (Exception ex)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("Không thể kết nối!\n" + ex.Message, "Thông báo!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
        }
        private void btnChapNhan_Click(object sender, EventArgs e)
        {
            if (CheckConnect())
            {
                SaveData();
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }
        }

        private void btnKiemTra_Click(object sender, EventArgs e)
        {
            if (CheckConnect())
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("Đã kết nối thành công!", "Thông báo!", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
}
